package com.example.ecas.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.beust.ah.A;
import com.example.ecas.pojo.DisRcase;
import jnr.ffi.annotations.In;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

// 病类-病历联系
@Repository
@Mapper
public interface DisRcaseMapper extends BaseMapper<DisRcase> {

    // 获取指定病类id的 病类-病历联系
    default List<DisRcase> getDisRcaseByDis(int disId) {
        QueryWrapper<DisRcase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dis_id", disId);
        return this.selectList(queryWrapper);
    }

    // 获取指定病历id的 病类-病历联系
    default List<DisRcase> getDisRcaseByCase(int caseId) {
        QueryWrapper<DisRcase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("case_id", caseId);
        return this.selectList(queryWrapper);
    }

    // 新增一个 病类-病历联系
    default int addDisRcase(DisRcase disRcase) {
        if (disRcase == null) {
            return 0;
        }
        return this.insert(disRcase);
    }

    // 删除一个联系
    default int deleteDisRcase(DisRcase disRcase) {
        QueryWrapper<DisRcase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dis_id", disRcase.getDisId()).eq("case_id", disRcase.getCaseId());
        return this.delete(queryWrapper);
    }

    // 根据caseId数组，获取去重的diseaseId
    default List<DisRcase> getDisList(List<DisRcase> disRcaseList) {
        List<Integer> caseIdList = disRcaseList.stream().map(DisRcase::getCaseId).collect(Collectors.toList());
        QueryWrapper<DisRcase> wrapper = new QueryWrapper<>();
        wrapper.select("distinct dis_id").in("case_id", caseIdList);
        return this.selectList(wrapper);
    }
}
